<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;
/**
 * 任务接口
 */
class Task extends Api
{
    protected $noNeedLogin = [''];
    protected $noNeedRight = ['*'];

    /**
     * 做任务
     *
     * @ApiMethod (POST)
     * @param string $type  任务类型：1=每日签到,2=每日首次分享,3=每日浏览10分钟	
     */
    public function doAddUserTesk()
    {
        $type = $this->request->post('type')?$this->request->post('type'):1;
        $msg = '';
        $system_tesk = DB::name("system_task")->find();
        if($type == 1){
            $user_task = Db::name('user_task')->where("(type = 1 or type = 2 or type = 3) and user_id = ".$this->auth->id." and create_time like '%".date("Y-m-d")."%'")->find();
            if($user_task){
                $this->error("您今天已签到，请明天再签");
            }
            $msg = '签到成功';
            $user = DB::name("user")->where("id",$this->auth->id)->find();
            
            if($user['signin_num'] == 7){
                //签到7天,从第一天开始
                $signin_num = $system_tesk['day_signin_num'];
                $memo = "签到获得".$signin_num."嘶豆";
                $signin_type = 1;
                $user_signin_num = 1;
            }else{
                //签到不足7天
                // 前一天的时间
                $yesterdayTime = strtotime("-1 day", time());
                // 格式化前一天的时间为Y-m-d H:i:s格式
                $yesterdayDate = date("Y-m-d", $yesterdayTime);
                $yester_user_task = Db::name('user_task')->where("(type = 1 or type = 2 or type = 3) and user_id = ".$this->auth->id." and create_time like '%".$yesterdayDate."%'")->find();
                // echo "type = 1 and create_time like '%".$yesterdayDate."%'";exit;
                if($yester_user_task){
                    //签到没断
                    if($user['signin_num'] == 2){
                        //签到断了,从第一天开始
                        $signin_num = $system_tesk['three_signin_num'];
                        $memo = "连续签到3天获得".$signin_num."嘶豆";
                        $signin_type = 2;
                        $user_signin_num = 3;
                    }elseif($user['signin_num'] == 6){
                        //签到断了,从第一天开始
                        $signin_num = $system_tesk['seven_signin_num'];
                        $memo = "连续签到7天获得".$signin_num."嘶豆";
                        $signin_type = 3;
                        $user_signin_num = 7;
                    }else{
                        //签到断了,从第一天开始
                        $signin_num = $system_tesk['day_signin_num'];
                        $memo = "签到获得".$signin_num."嘶豆";
                        $signin_type = 1;
                        $user_signin_num = $user['signin_num']+1;
                    }
                }else{
                    //签到断了,从第一天开始
                    $signin_num = $system_tesk['day_signin_num'];
                    $memo = "签到获得".$signin_num."嘶豆";
                    $signin_type = 1;
                    $user_signin_num = 1;
                }
            }
           
            //更新签到次数
            DB::name("user")->where("id",$this->auth->id)->update(array("signin_num"=>$user_signin_num));
            //更新余额
            DB::name("user")->where("id",$this->auth->id)->setInc("money",$signin_num);
            //插入流水记录
            $money_log = array(
                "user_id" => $this->auth->id,
                "money" => $signin_num,
                "before" => $user['money'],
                "after" => $user['money']+$signin_num,
                "memo" => $memo,
                "createtime" => time()
            );
            DB::name("user_money_log")->insert($money_log);
            //插入签到记录
            $tesk_log = array(
                "user_id" => $this->auth->id,
                "num" => $signin_num,
                "type" => $signin_type,
                "create_time" => date("Y-m-d H:i:s")
            );
            DB::name("user_task")->insert($tesk_log);
        }elseif($type == 2){
            $user_task = Db::name('user_task')->where("type = 4 and user_id = ".$this->auth->id." and create_time like '%".date("Y-m-d")."%'")->find();
            if($user_task){
                $this->error("您今天已领取分享嘶豆，请明天分享");
            }
            $msg = '分享成功';
            $user = DB::name("user")->where("id",$this->auth->id)->find();
            //签到7天,从第一天开始
            $signin_num = $system_tesk['share_num'];
            $memo = "分享获得".$signin_num."嘶豆";
            //更新余额
            DB::name("user")->where("id",$this->auth->id)->setInc("money",$signin_num);
            //插入流水记录
            $money_log = array(
                "user_id" => $this->auth->id,
                "money" => $signin_num,
                "before" => $user['money'],
                "after" => $user['money']+$signin_num,
                "memo" => $memo,
                "createtime" => time()
            );
            DB::name("user_money_log")->insert($money_log);
            //插入签到记录
            $tesk_log = array(
                "user_id" => $this->auth->id,
                "num" => $signin_num,
                "type" => 4,
                "create_time" => date("Y-m-d H:i:s")
            );
            DB::name("user_task")->insert($tesk_log);
        }elseif($type == 3){
            $user_task = Db::name('user_task')->where("type = 6 and user_id = ".$this->auth->id." and create_time like '%".date("Y-m-d")."%'")->find();
            if($user_task){
                $this->error("每日浏览10分钟今日已获得嘶豆，请明天再来");
            }
            $msg = '分享成功';
            $user = DB::name("user")->where("id",$this->auth->id)->find();
            $signin_num = $system_tesk['look_num'];
            $memo = "浏览10分钟获得".$signin_num."嘶豆";
            //更新余额
            DB::name("user")->where("id",$this->auth->id)->setInc("money",$signin_num);
            //插入流水记录
            $money_log = array(
                "user_id" => $this->auth->id,
                "money" => $signin_num,
                "before" => $user['money'],
                "after" => $user['money']+$signin_num,
                "memo" => $memo,
                "createtime" => time()
            );
            DB::name("user_money_log")->insert($money_log);
            //插入签到记录
            $tesk_log = array(
                "user_id" => $this->auth->id,
                "num" => $signin_num,
                "type" => 6,
                "create_time" => date("Y-m-d H:i:s")
            );
            DB::name("user_task")->insert($tesk_log);
        }else{
            $this->error("状态有误");
        }
        $this->success($msg);
    }

    /**
     * 我的签到
     * @param string $start_time 开始日期
     * @param string $end_time 结束日期
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", description="扩展数据返回")
     * @ApiReturnParams   (name="name", type="object", description="收货人姓名")
     * @ApiReturnParams   (name="mobile", type="object", description="手机号")
     * @ApiReturnParams   (name="area", type="object", description="地址")
     * @ApiReturnParams   (name="type", type="object", description="类型:1=默认,2=非默认")
     * @ApiReturnParams   (name="address", type="object", description="详细地址")
     *
     */
    public function getUserSignin()
    {
        $start_time = input("start_time");
        $end_time = input("end_time");
        $token = $this->auth->getToken();
        $user = \app\common\library\Token::get($token);
        $result = array();
        for ($i=strtotime($start_time); $i<=strtotime($end_time); $i+=86400)
        {
            $time_arr[] = date("Y-m-d", $i);
            
        } 
        
        foreach($time_arr as $key => $val){
            $user_signin = Db::name('user_task')->where("(type = 1 or type = 2 or type = 3) and user_id = ".$this->auth->id." and create_time like '%".$val."%'")->find();
            if($user_signin){
                $result[$key]['is_signin'] = 1;
            }else{
                $result[$key]['is_signin'] = 2;
            }
            $result[$key]['signin_time'] = $val;
        }

        $this->success('请求成功',$result);
    }
}
